﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using Ninject;
using ETA.IService;
using ETA.Dtos;
using ETA.DtoMappings;
using ETA.Entity;

namespace ETA.UnitTest
{
    [TestFixture]
    public class UserServiceTest
    {
        private static IUserService _IUserService { set; get; }
        private static string pwd = "123456";

        [SetUp]
        public void Init()
        {
            DtoMapConfig.InitMap();
            IKernel kernel = new StandardKernel(new NinjectModuleBuild());
            _IUserService = kernel.Get<IUserService>();
        }

        [Test]
        public void RegisterTest()
        {
            var rosponse = _IUserService.QueryRoler(new QueryRolerRequest());
            var rolers = rosponse.Rolers.FirstOrDefault(r => r.RolerName == "实习生");
            var dto = new UserDto
            {
                UserId = Guid.NewGuid() + "",
                Email = "longfeiit@127.com",
                Mobile = "18221647822",
                MobileActive = true,
                Password = pwd,
                UserName = "小鱼儿",
                Rolers = new List<Roler> { rolers }
            };
            var createUserReq = new CreateUserRequest
            {
                UserDto = dto
            };
            var createUserResponse = _IUserService.AddUser(createUserReq);
            Assert.True(createUserResponse.Result.IsSuccess);
        }

        [Test]
        public void AddAdmin()
        {
            var rosponse = _IUserService.QueryRoler(new QueryRolerRequest());
            var rolers = rosponse.Rolers.FirstOrDefault(r => r.RolerName == "网站后台管理员");
            var dto = new UserDto
            {
                UserId = Guid.NewGuid() + "",
                Email = "longfeiit@127.com",
                Mobile = "18221647822",
                MobileActive = true,
                Password = pwd,
                UserName = "超级管理员",
                Rolers = new List<Roler> { rolers },
                LoginName = "admin"
            };
            var createUserReq = new CreateUserRequest
            {
                UserDto = dto
            };
            var createUserResponse = _IUserService.AddUser(createUserReq);
            Assert.True(createUserResponse.Result.IsSuccess);
        }

        [Test]
        public void AddSosAdmin()
        {
            var rosponse = _IUserService.QueryRoler(new QueryRolerRequest());
            var rolers = rosponse.Rolers.FirstOrDefault(r => r.RolerName == "援助事物管理员");
            var dto = new UserDto
            {
                UserId = Guid.NewGuid() + "",
                Email = "longfeiit@128.com",
                Mobile = "18221647823",
                MobileActive = true,
                Password = pwd,
                UserName = "援助事物管理员",
                Rolers = new List<Roler> { rolers },
                LoginName = "sadmin"
            };
            var createUserReq = new CreateUserRequest
            {
                UserDto = dto
            };
            var createUserResponse = _IUserService.AddUser(createUserReq);
            Assert.True(createUserResponse.Result.IsSuccess);
        }

        [Test]
        public void AddRegistHandleAdmin()
        {
            var rosponse = _IUserService.QueryRoler(new QueryRolerRequest());
            var rolers = rosponse.Rolers.FirstOrDefault(r => r.RolerName == "报名事物处理员");
            var dto = new UserDto
            {
                UserId = Guid.NewGuid() + "",
                Email = "longfeiit@129.com",
                Mobile = "18221647824",
                MobileActive = true,
                Password = pwd,
                UserName = "报名事物处理员",
                Rolers = new List<Roler> { rolers },
                LoginName = "radmin"
            };
            var createUserReq = new CreateUserRequest
            {
                UserDto = dto
            };
            var createUserResponse = _IUserService.AddUser(createUserReq);
            Assert.True(createUserResponse.Result.IsSuccess);
        }

        [Test]
        public void AddFranchiseAdmin()
        {
            var rosponse = _IUserService.QueryRoler(new QueryRolerRequest());
            var rolers = rosponse.Rolers.FirstOrDefault(r => r.RolerName == "财务管理员");
            var dto = new UserDto
            {
                UserId = Guid.NewGuid() + "",
                Email = "longfeiit@130.com",
                Mobile = "18221647814",
                MobileActive = true,
                Password = pwd,
                UserName = "财务管理员",
                Rolers = new List<Roler> { rolers },
                LoginName = "cadmin"
            };
            var createUserReq = new CreateUserRequest
            {
                UserDto = dto
            };
            var createUserResponse = _IUserService.AddUser(createUserReq);
            Assert.True(createUserResponse.Result.IsSuccess);
        }

        [Test]
        public void GetUserTest()
        {
            var req = new QueryUserRequest
            {
                User = new User
                {
                    LoginName = "skyforce2008"
                }
            };
            var response = _IUserService.QueryUser(req);
            Assert.AreEqual(response.Users.FirstOrDefault().UserName, "孙龙飞");
        }

        [Test]
        public void TestSendRegistSms()
        {
            var res = _IUserService.SendCheckNum("18221647820");
        }

        [Test]
        public void TestGenerateNo()
        {
            var res = _IUserService.GenerateNo("U",1);
            Assert.True(!string.IsNullOrEmpty(res));
        }

        [Test]
        public void TestGetFilterUser()
        {
            int rowCount = 0;
            var res = _IUserService.GetFilterUser(out rowCount, new ETA.ExtendEntity.UserFilter {Page=1,PageSize=20 });
            Assert.True(rowCount>0);
        }
    }
}
